var express = require('express');
const AccountModel = require('../../model/accountModel');
const { error } = require('console');
let checkTokenMiddleware = require('../../middleware/checkTokenMiddleware');

var router = express.Router();


//生读取全部数据
router.get('/account', checkTokenMiddleware, function (req, res, next) {
    // let accounts = db.get('account').value();
    console.log(req.user);

    let accounts = AccountModel.find().sort({ date: -1 }).exec();
    accounts.then((data) => {

        res.json(
            {
                code: '0000',
                msg: '读取成功',
                data: data
            }
        )
    }).catch(() => {
        res.json(
            {
                code: '1001',
                meg: "读取失败",
                data: null
            }
        )
    })

});

//读取单条数据
router.get('/account/:id', checkTokenMiddleware, (request, response) => {
    let { id } = request.params;
    console.log(id);
    AccountModel.find({ _id: id }).then((data) => {
        response.json(
            {
                code: '0000',
                msg: '读取成功',
                data: data
            }
        )
    }).catch(() => {
        response.json(
            {
                code: '1002',
                msg: '读取失败',
                data: null
            }
        )
    })
})
//添加数据
router.post('/account', checkTokenMiddleware, (req, res, next) => {

    req.body.date = new Date(Date.parse(req.body.date));

    let result = AccountModel.create({ ...req.body });

    result.then((data) => {
        res.json(
            {
                code: '0000',
                msg: '添加成功',
                data: data
            }
        )
    }).catch(() => {
        res.json(
            {
                code: '1003',
                msg: '添加失败',
                data: null
            }
        )
    })
})

//删除数据
router.delete('/account/:id', checkTokenMiddleware, (req, res, next) => {
    let id = req.params.id;


    let result = AccountModel.deleteOne({ _id: id });
    result.then((data) => {
        res.json(
            {
                code: '0000',
                msg: '删除成功',
                data: {}
            }
        )
    }).catch(() => {
        res.json(
            {
                code: '1004',
                msg: '删除失败',
                data: null
            }
        )
    })
})

//修改数据
router.patch('/account/:id', checkTokenMiddleware, (req, res) => {
    let { id } = req.params;

    let result = AccountModel.updateOne({ _id: id }, req.body)
    result.then(() => {

        AccountModel.find({ _id: id })
            .then((data) => {
                res.json(
                    {
                        code: '0000',
                        msg: '修改成功',
                        data: data
                    }
                )
            })
    }).catch(() => {
        res.json(
            {
                code: '1005',
                msg: '修改失败',
                data: null
            }
        )
    })
})
module.exports = router;
